package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;

import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;

import com.qk.management.service.CourseService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/subject/{subject}")
    public Result getCoursesBySubject(@PathVariable("subject") Integer subject) {
        return Result.success(courseService.getCoursesBySubject(subject));
    }

    @GetMapping("/list")
    public Result getCourses() {
        return Result.success(courseService.getCourses());
    }


   @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        return courseService.updateCourse(course)==1 ? Result.error("未找到该id") : Result.success();
    }


    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable("id") Integer id) {
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }


    @DeleteMapping("/{id}")
    public  Result deleteCourseById(@PathVariable("id") Integer id) {
        courseService.deleteCourseById(id);
        return Result.success();
    }


    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        courseService.addCourse(course);
        return Result.success();
    }


    /**
     * 条件分页查询用户列表
     */
    @GetMapping
    public Result page(CourseDTO dto) {

        PageResult<Course> courses=courseService.page(dto);
        return Result.success(courses);
    }

}
